package ms.platform.schedule;

import ms.platform.email.ExchangeMailService;
import ms.platform.email.MailService;
import ms.platform.plan.dto.ModelDto;
import ms.platform.progress.service.FactoryPlanService;
import ms.platform.system.dto.UserMailDto;
import ms.platform.system.model.Admin;
import ms.platform.system.service.AdminService;
import ms.platform.system.service.IndexService;
import ms.platform.util.DateUtil;
import ms.platform.util.HttpUtils;
import ms.platform.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.mail.MessagingException;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TimingSchedule {

    @Autowired
    FactoryPlanService factoryService;
    @Autowired
    private IndexService indexService;
    @Autowired
    private MailService mailService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private ExchangeMailService exchangeMailService;
    private Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    //定时执行的方法
    public void execute() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        SimpleDateFormat dfNew = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        logger.error("begin implement job-------------" + dfNew.format(new Date()));
        //所有厂商账号
        String rDate = DateUtil.getDateString(new Date(), "yyyy-MM-dd");
        int week = DateUtil.dayForWeek(rDate);
//        if(week ==2 || week == 5){//周二，周五触发发送邮件
        	List<UserMailDto> listadmin = adminService.getUserMailDto();
            for (UserMailDto user : listadmin) {
                //获取邮箱
                //如果邮箱为空,直接返回
                if (StringUtil.isNullOrBlock(user.getEmail())) {
                    continue;
                }
                String email = user.getEmail();
                String linkMail = user.getLinkMail();
                String linkMailTwo = user.getLinkMailTwo();
//                String linkMailThree = user.getLinkMailThree();
//                String linkMailFour = user.getLinkMailFour();
                String linkMailThree = "wangchaoqun@ghapii.com.cn";
                String linkMailFour = "yanxifeng@ghapii.com.cn";
                List<ModelDto> list = factoryService.myTimingScheduleList(user.getId().toString());
                String newDate = df.format(new Date());
                if (list != null && list.size() > 0) {
                    for (ModelDto modelDto : list) {
                        if (modelDto.getReportFreq() == null) {
                            continue;
                        }
                        //状态为"已下发"才发送
                        if(!"1".equals(modelDto.getPlanStatus())){
                            continue;
                        }
                        //定时每天中午12点触发
                        //获取频次（几天触发一次）
                        int num = modelDto.getReportFreq();
                        //如果汇报时间为空直接发送
                        if (StringUtil.isNullOrBlock(modelDto.getLastReportTime())) {
                            this.sendEmail(modelDto, email,linkMail,linkMailTwo,linkMailThree,linkMailFour, dfNew);
                            continue;
                        }
                        //获取汇报日期
                        String date = modelDto.getLastReportTime();
                        //汇报日期-当前时间 >= 频次 即发送
                        long day = DateUtil.middate(newDate, date, df);
                        if (day >= num) {
                            this.sendEmail(modelDto, email,linkMail,linkMailTwo,linkMailThree,linkMailFour, dfNew);
                        }
                    }
                }
            }
//        }
        logger.error("执行时间" + dfNew.format(new Date()));
    }

    //进行发送邮件
    private void sendEmail(ModelDto modelDto, String email,String linkMail,String linkMailTwo,String linkMailThree,String linkMailFour, SimpleDateFormat dfNew) {
        //进行发送邮件
//        String date = dfNew.format(new Date());
        try {
//            mailService.sendHtmlMail(email, "进度提醒", "亲爱的厂商:<br/>您好!" +
//                    "<br/>机种:" + modelDto.getModelType() + " 部品番号:" + modelDto.getModelCode() + ",难易度:" + modelDto.getModelLevel()
//                    + ",所属客户:" + modelDto.getCustomer() + ";<br/>请尽快提交进度!");
            exchangeMailService.send("进度提醒",new String[]{email},new String[]{linkMailThree,linkMailFour},"亲爱的厂商:<br/>您好!"
                    +"<br/>机种:" + modelDto.getModelType() + " 部品番号:" + modelDto.getModelCode() + ",难易度:" + modelDto.getModelLevel()
                   + ",所属客户:" + modelDto.getCustomer() + ";<br/>请尽快提交进度!");
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
